//
//  VideoViewController.m
//  ToYourHeartNews
//
//  Created by XJing on 16/1/17.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥. All rights reserved.
//

#import "VideoViewController.h"
#import <AFNetworking.h>
#import "AfNetWorkingHandler.h"
#import "KindModel.h"
#import "Videomodel.h"
#import "KindCell.h"
#import "VideoTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "KindViewController.h"
#import <MJRefresh.h>
#import "VideoBigViewController.h"

@interface VideoViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, passVideoDelegate>
@property (nonatomic, strong)NSMutableArray *ListArr;//下面列表的数据
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *kindArr;//上面分类的数据
@property (nonatomic, strong)UICollectionView *collection;
@property (nonatomic, assign)NSInteger number;//请求数据的个数
@end

@implementation VideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"视频";
    //数据处理
    [self dataHandler];
    //tableView
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[VideoTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    [_tableView release];
    //tableView头部区域
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, (self.view.frame.size.height - 64) / 6 )];
    view.backgroundColor = [UIColor magentaColor];
    _tableView.tableHeaderView = view;
    [view release];
    //collection
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumInteritemSpacing = 1;
    layout.minimumLineSpacing = 1;
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 3) / 4, (self.view.frame.size.height - 64) / 6 - 10);
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, (self.view.frame.size.height - 64) / 6) collectionViewLayout:layout];
    [_collection registerClass:[KindCell class] forCellWithReuseIdentifier:@"kindcell"];
    _collection.dataSource = self;
    _collection.delegate = self;
    _collection.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:_collection];
    [layout release];
    [_collection release];
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(dataHandler)];
    [_tableView.mj_header beginRefreshing];
    //上拉加载
    self.number = 10;
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadnewdata)];
    if (_ListArr.count != 0) {
        [_tableView.mj_footer beginRefreshing];
    }
    VideoTableViewCell *cell = [[VideoTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"haha"];
    cell.delegate = self;
    
    //隐藏上下边栏
    //是否实现了方法
    if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]) {
        self.automaticallyAdjustsScrollViewInsets = NO;//取消scrollview的内容自动调整
        UIEdgeInsets insets = self.tableView.contentInset ;//tableView边界预留
        insets.top = self.navigationController.navigationBar.bounds.size.height;//让上边界的预留值和导航栏相等
//        self.tableView.contentInset = insets;
        self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
        //把修改后的边界预留值赋值给tableView
        self.tableView.scrollIndicatorInsets = insets;//滚动指标点到滚动视图的边缘的距离
        
    }

    
}
//隐藏上下边栏
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    if (velocity.y > 0) {
        self.navigationController.navigationBar.hidden = YES;
        self.tabBarController.tabBar.hidden = YES;
    }else{
        self.navigationController.navigationBar.hidden = NO;
        self.tabBarController.tabBar.hidden = NO;
    }
}
-(void)passVideo
{
    VideoBigViewController *bigVC = [[VideoBigViewController alloc]init];
    [self.navigationController pushViewController:bigVC animated:YES];
}
//上拉加载
- (void)loadnewdata
{
    NSString *str = [NSString stringWithFormat:@"http://c.m.163.com/nc/video/home/%ld-10.html", (long)_number];
    [AfNetWorkingHandler GetWithStr:str complection:^(id result) {
        //
        NSLog(@"%@", result);
        NSArray *arr = result[@"videoSidList"];
        self.kindArr = [KindModel modelHanderWithArray:arr];
        [_collection reloadData];
        NSArray *arr1 = result[@"videoList"];
        NSArray *arrTemp = [Videomodel modelHanderWithArray:arr1];;
        for (NSInteger i = 0; i < 10; i++) {
            [_ListArr addObject:arrTemp[i]];
        }
        
        [_tableView reloadData];
        NSLog(@"%@", _ListArr);
    }];
    
    [_tableView.mj_footer endRefreshing];
    self.number += 10;
}

//数据处理
-(void)dataHandler
{
    NSString *str = @"http://c.m.163.com/nc/video/home/0-10.html";
    [AfNetWorkingHandler GetWithStr:str complection:^(id result) {
        //
        NSLog(@"%@", result);
        NSArray *arr = result[@"videoSidList"];
        self.kindArr = [KindModel modelHanderWithArray:arr];
        [_collection reloadData];
        NSArray *arr1 = result[@"videoList"];
        self.ListArr = [Videomodel modelHanderWithArray:arr1];
        [_tableView reloadData];
        NSLog(@"%@", _ListArr);
    }];
    [_tableView.mj_header endRefreshing];
}
//创建collection个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _kindArr.count;
}
//点击进入分类详情
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    KindModel *m = _kindArr[indexPath.row];
    KindViewController *kindVC = [[KindViewController alloc]init];
    kindVC.sid = m.sid;
    [self.navigationController pushViewController:kindVC animated:YES];
    [kindVC release];
}
//collection中的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    KindCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"kindcell" forIndexPath:indexPath];
    KindModel *m = _kindArr[indexPath.row];
    [cell.picture sd_setImageWithURL:[NSURL URLWithString:m.imgsrc]];
    cell.title.text = m.title;
    return cell;
}
//创建tableView个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _ListArr.count;
}
//tableView的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 300;
}
//tableView中的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    VideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    Videomodel *m = _ListArr[indexPath.row];
    cell.titleLable.text = m.title;
    cell.detailLable.text = m.description1;
    //将时间转换成00:00形式
    NSNumber *n = m.length;
    if (n.integerValue < 60) {
        cell.timeLable.text = [NSString stringWithFormat:@"00: %ld", n.integerValue];
    }else{
    NSInteger fen = n.integerValue / 60;
    NSInteger miao = n.integerValue - 60 * fen;
    NSString *time = [NSString stringWithFormat:@"%ld : %ld", fen, miao];
    cell.timeLable.text = time;
    }
    //将播放次数转换成0.0万形式
    NSNumber *play = m.playCount;
    NSInteger wan = play.integerValue / 10000;
    NSInteger qian = (play.integerValue - wan * 10000) / 1000;
    cell.playLable.text = [NSString stringWithFormat:@"%ld.%ld万", wan, qian];
    cell.str = m.mp4_url;
    cell.commentLable.text = [m.replyCount.stringValue stringByAppendingString:@"评论"];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
